package com.mytest.provider.service.config.mq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * fanout 类型
 * @Author: llq
 * @Date: 2020/4/29
 */
@Configuration
public class mqConfig {
    /**
     * 定义死信队列相关信息
     */
    public final static String deadQueueName = "dead_queue";
    public final static String deadRoutingKey = "dead_routing_key";
    public final static String deadExchangeName = "dead_exchange";
    /**
     * 死信队列的交换机标识符
     */
    public static final String DEAD_LETTER_QUEUE_KEY = "x-dead-letter-exchange";
    /**
     * 死信队列交换机绑定键标识符
     */
    public static final String DEAD_LETTER_ROUTING_KEY = "x-dead-letter-routing-key";

    /**
     * ttl
     */
    public static final String X_MESSAGE_TTL = "x-message-ttl";

    /**
     * 邮件队列
     */
    private String FANOUT_EMAIL_QUEUE = "fanout_email_queue";

    //
    /**
     * 短信队列
     */
    private String FANOUT_SMS_QUEUE = "fanout_sms_queue";

    /**
     * fanout 交换机
     */
    private String EXCHANGE_NAME = "fanoutExchange";

    /**
     * x-delayed-message 延迟交换机 ************************
     */
    private String DELAYED_EXCHANGE_NAME = "delayedExchange";

    /**
     * 1.定义邮件队列
     * @return
     */
    @Bean
    public Queue fanOutEamilQueue() {
        // 将普通队列赋予死信交换机上 ******************************
        Map<String, Object> args = new HashMap<>(2);
        args.put(DEAD_LETTER_QUEUE_KEY, deadExchangeName);
        args.put(DEAD_LETTER_ROUTING_KEY, deadRoutingKey);
//        这里不配置延迟时间，会报错reply-code=406
//        args.put(X_MESSAGE_TTL, 10000);

        Queue queue = new Queue(FANOUT_EMAIL_QUEUE, true, false, false, args);
        return queue;
    }

    /**
     * 2.定义短信队列
     * @return
     */
    @Bean
    public Queue fanOutSmsQueue() {
        return new Queue(FANOUT_SMS_QUEUE);
    }

    /**
     * 2-1.定义延迟交换机
     * x-delayed-message ： 延迟交换机类型   *************************
     * @return
     */
    @Bean
    CustomExchange delayedExchange() {
        return new CustomExchange(DELAYED_EXCHANGE_NAME,"x-delayed-message");
    }
    /**
     * 2-2.定义交换机
     * @return
     */
    /*@Bean
    DirectExchange fanoutExchange() {
        return new DirectExchange(EXCHANGE_NAME);
    }*/

    /**
     * 3.队列与交换机绑定邮件队列
     * 队列与交换机互相绑定
     * @return
     */
    @Bean
    Binding bindingExchangeEamil() {
        return BindingBuilder.bind(fanOutEamilQueue()).to(delayedExchange()).with("email.x").noargs();
    }
    /*@Bean
    Binding bindingExchangeEamil() {
        return BindingBuilder.bind(fanOutEamilQueue()).to(fanoutExchange()).with("email.x");
    }*/

    /**
     * 4.队列与交换机绑定短信队列
     * 队列与交换机互相绑定
     * @return
     */
    @Bean
    Binding bindingExchangeSms() {
        return BindingBuilder.bind(fanOutSmsQueue()).to(delayedExchange()).with("sms.x").noargs();
    }
   /* @Bean
    Binding bindingExchangeSms() {
        return BindingBuilder.bind(fanOutSmsQueue()).to(fanoutExchange()).with("sms.x");
    }*/


    /**
     * 创建配置死信邮件队列
     *
     * @return
     */
    @Bean
    public Queue deadQueue() {
        Queue queue = new Queue(deadQueueName, true);
        return queue;
    }


    /**
     * 创建死信交换机
     * @return
     */
    @Bean
    public DirectExchange deadExchange() {
        return new DirectExchange(deadExchangeName);
    }


    /**
     * 死信队列与死信交换机绑定
     * @return
     */
    @Bean
    public Binding bindingDeadExchange() {
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with(deadRoutingKey);
    }


}
